package com.atmaowei.controller;

import com.atmaowei.common.Result;
import com.atmaowei.controller.request.StudentPageRequest;
import com.atmaowei.entity.Department;
import com.atmaowei.entity.Student;
import com.atmaowei.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;


    @PostMapping("/save")
    public Result save(@RequestBody Student student) {
        studentService.save(student);
        return Result.success();
    }

    @DeleteMapping("/delete/{sno}")
    public Result delete(@PathVariable String sno) {
        studentService.delete(sno);
        return Result.success();
    }

    @PutMapping("/update/{oldSno}")
    public Result update(@RequestBody Student student,@PathVariable String oldSno) {
        studentService.update(student,oldSno);
        return Result.success();
    }

    @GetMapping("/{sno}")
    public Result getBySno(@PathVariable String sno) {
        Student student = studentService.getBySno(sno);
        return Result.success(student);
    }

    @GetMapping("/getAllDept")
    public Result getAllDept() {
        List<Department> departments = studentService.getAllDept();
        return Result.success(departments);
    }

    @GetMapping("/page")
    public Result page(StudentPageRequest studentPageRequest) {
        return Result.success(studentService.page(studentPageRequest));
    }

    @PostMapping("/deleteSelected")
    public Result deleteSelected(@RequestBody List<Student> students) {
        studentService.deleteSelected(students);
        return Result.success();
    }

    @GetMapping("/statusChange/{sno}")
    public Result statusChange(@PathVariable String sno) {
        studentService.statusChange(sno);
        return Result.success();
    }

}
